package org.txazo.music.commons.mail;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.txazo.music.util.md5.MD5;

/**
 * 注册认证邮件
 * 
 * @author Tu XiaoZhou
 * @since 2013-03-31
 */
public class RegisterConfirmMail implements Mail {

	public static String mail_content_1 = null;
	public static String mail_content_2 = null;
	public static String mail_content_3 = null;

	static {
		mail_content_1 = "<html lang=\"zh\"><head>"
				+ "<meta charset=\"UTF-8\" /></head><body>"
				+ "<div style=\"color:#339999; font-size:14px;\">"
				+ "亲爱的用户，你好:<br /><br /><br />"
				+ "请您点击下面链接来激活您的听享音乐邮箱帐号:<br />"
				+ "<a target=\"_blank\" href=\"http://www.txazo.com/register/confirm?r=";
		mail_content_2 = "\">http://www.txazo.com/register/confirm?r=";
		mail_content_3 = "</a><br /><br /><br />"
				+ "为了确保您的帐号安全，该链接仅在"
				+ "<font color=\"red\">24小时内</font>访问有效。<br /><br />"
				+ "激活成功后，即可登录<font color=\"#ff6633\">听享音乐</font>"
				+ "享受各种音乐服务。<br /><br /><br />"
				+ "如果点击链接不工作...<br /><br />"
				+ "请您选择并复制整个链接，打开浏览器窗口并将其粘贴到地址栏中，"
				+ "然后单击\"转到\"按钮或按键盘上的 Enter 键。<br /><br /><br />"
				+ "<font color=\"red\">请勿直接回复该邮件</font>，有关"
				+ "<font color=\"#ff6633\">听享音乐</font>的更多帮助信息，请访问："
				+ "<a target=\"_blank\" href=\"http://www.txazo.com/help.html\">"
				+ "http://www.txazo.com/help.html</a><br /><br /><br />"
				+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
				+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
				+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
				+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
				+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
				+ "<font color=\"#ff6633\">听&nbsp享&nbsp音&nbsp乐</font><br />"
				+ "</div></body></html>";
	}

	public static String getMailContent(String account) {
		String encryptAccount = MD5.md5OfHex(account).toLowerCase();
		StringBuilder sb = new StringBuilder(1300);
		sb.append(mail_content_1);
		sb.append(encryptAccount);
		sb.append(mail_content_2);
		sb.append(encryptAccount);
		sb.append(mail_content_3);

		return sb.toString();
	}

	/** 发送注册认证邮件 */
	@Override
	public void sendMail(JavaMailSender sender, String to)
			throws MessagingException {
		MimeMessage message = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setTo(to);
		helper.setFrom("txazo_music_1218@163.com");
		helper.setSubject("听享音乐 — 邮箱验证");

		helper.setText(getMailContent(to), true);

		sender.send(message);
	}

}